package com.oa.modules.approvalsec1.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.Constant;
import com.oa.modules.approvalsec1.dao.ProjectScheduleDao;
import com.oa.modules.approvalsec1.entity.ProjectScheduleEntity;
import com.oa.modules.approvalsec1.service.ProjectScheduleService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service("projectScheduleService")
public class ProjectScheduleServiceImpl extends ServiceImpl<ProjectScheduleDao, ProjectScheduleEntity> implements ProjectScheduleService {


    /**
     * 获取所有节点列表
     */
    @Override
    public List<ProjectScheduleEntity> getAllScheduleList(List<Integer> ProjectScheduleList){
        //查询根节点列表
        List<ProjectScheduleEntity> scheduleList = queryListParentId(0, ProjectScheduleList);
        //递归获取子节点
        getMenuTreeList(scheduleList, ProjectScheduleList);

        scheduleList.forEach(item->{
            List<ProjectScheduleEntity> children = (List<ProjectScheduleEntity>) item.getChildren();
            children.forEach(i->{
                List<ProjectScheduleEntity> child = baseMapper.selectList(new QueryWrapper<ProjectScheduleEntity>().eq("parent_id", i.getId()));
                if(child!=null && child.size()>0){
                    i.setChildren(child);
                }
            });
        });

        return scheduleList;
    }

    /**
     * 递归
     */
    private List<ProjectScheduleEntity> getMenuTreeList(List<ProjectScheduleEntity> scheduleList, List<Integer> scheduIdList){
        List<ProjectScheduleEntity> subScheduleList = new ArrayList<>();

        for(ProjectScheduleEntity entity : scheduleList){
            //目录
            if(entity.getParentId() == Constant.MenuType.CATALOG.getValue()){
                entity.setChildren(getMenuTreeList(queryListParentId(entity.getId(), scheduIdList), scheduIdList));
            }
            subScheduleList.add(entity);
        }

        return subScheduleList;
    }

    public List<ProjectScheduleEntity> queryListParentId(Integer parentId) {
        return baseMapper.queryListParentId(parentId);
    }
    public List<ProjectScheduleEntity> queryListParentId(Integer parentId, List<Integer> ProjectScheduleList) {
        List<ProjectScheduleEntity> scheduleList = queryListParentId(parentId);
        if(ProjectScheduleList == null){
            return scheduleList;
        }
        //
        List<ProjectScheduleEntity> proScheuleList = new ArrayList<>();
        for(ProjectScheduleEntity entity : scheduleList){
            //
            if(ProjectScheduleList.contains(entity.getId())){
                proScheuleList.add(entity);
            }
        }
        return proScheuleList;
    }
}
